package com.xkcoding.orm.mybatis.plus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xkcoding.orm.mybatis.plus.entity.User;
import com.xkcoding.orm.mybatis.plus.mapper.UserMapper;
import com.xkcoding.orm.mybatis.plus.service.RoleService;
import com.xkcoding.orm.mybatis.plus.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * <p>
 * User Service
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2018-11-08 18:10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final RoleService roleService;

    /**
     * 这里没事务，通过this调用的也没有事务，无论this调用的方法是否有事务!
     * 入口必须有事务，后面才有事务
     */
    //@Transactional(rollbackFor = Exception.class)
    @Override
    public void trans() {
        log.info("user" + String.valueOf(TransactionSynchronizationManager.isActualTransactionActive()));
        for (int i = 0; i < 5; i++) {
            this.transs();
        }
        //roleService.trans();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void transs() {
        log.info("user  transs  " + String.valueOf(TransactionSynchronizationManager.isActualTransactionActive()));
        roleService.trans();
    }
}
